/**
 * Transfer.java
 */
package fss.transfer;

import java.io.Serializable;
import java.util.Date;

import fss.util.UtcDate;

/**
 * A transfer object between Remote File Service and Remote File
 * Service Client.
 * <p>
 * Represents both Download and Upload data. To check which type
 * inspect the Transfer Identifier.
 * 
 * @author thinh
 *
 */
public class Transfer implements Serializable  {
    private static final long serialVersionUID = 1L;
    /**
     * The timestamp this transfer was initiated (utc).
     */
    protected Date timestamp = null;
    /**
     * The last update timstamp.
     */
    protected Date lastUpdateTimestamp = null;
    /**
     * The unique transfer identifier.
     */
    protected TransferId id = null;
    /**
     * The status of this transfer.
     */
    protected StatusType status = null;
    /**
     * The file data that this status holds (chunks).
     */
    private byte[] data = null;
    /**
     * The total file size in bytes.
     */
    private long filesize = -1;
    /**
     * The file offset byte for this download status.
     */
    private int offset = 0;
    /**
     * The sequence of the transfer.
     */
    private int sequence = 0;
    /**
     * Constructor initiailizing the creation timestamp.
     */
    private Transfer() {
        timestamp = (new UtcDate()).getDate();
        this.status = StatusType.INITIALIZED;
        data = new byte[0]; // empty buffer
        filesize = 0;
        offset = 0;
    }
    /**
     * Constructor with specified transfer identifier.
     * @param id the id.
     */
    public Transfer(TransferId id) {
        this();
        this.id = id;
    }
    /**
     * Get the creation timestamp.
     * @return the timestamp.
     */
    public Date getTimestamp() {
        return timestamp;
    }
    /**
     * Get the last update timestsamp.
     * @return the timestamp.
     */
    public Date getLastUpdateTimestamp() {
        return lastUpdateTimestamp;
    }
    /**
     * Set the last update timestamp.
     * @param lastUpdateTimestamp the value.
     */
    public void setLastUpdateTimestamp(Date lastUpdateTimestamp) {
        this.lastUpdateTimestamp = lastUpdateTimestamp;
    }
    /**
     * Get the transfer id.
     * @return
     */
    public TransferId getId() {
        return id;
    }
    /**
     * Set the transfer id (package private).
     * @param id the id.
     */
    void setId(TransferId id) {
        this.id = id;
    }
    /**
     * Get the status.
     * @return the status.
     */
    public StatusType getStatus() {
        return status;
    }
    /**
     * Set the status.
     * @param status the value.
     */
    public void setStatus(StatusType status) {
        this.status = status;
    }
    /**
     * Get the file data chunk.
     * @return the byte array.
     */
    public byte[] getData() {
        return data;
    }
    /**
     * Set the data byte array.
     * @param data the byte array.
     */
    public void setData(byte[] data) {
        this.data = data;
    }
    /**
     * Get the total file size in bytes.
     * @return the size.
     */
    public long getFilesize() {
        return filesize;
    }
    /**
     * Set the total file size in bytes.
     * @param filesize the size.
     */
    public void setFilesize(long filesize) {
        this.filesize = filesize;
    }
    /**
     * Get the offset of the encapsulated data array
     * from the original file.
     * @return the offset.
     */
    public int getOffset() {
        return offset;
    }
    /**
     * Set the offset.
     * @param offset the offset.
     */
    public void setOffset(int offset) {
        this.offset = offset;
    }
    /**
     * Get the sequence of the transfer.
     * @return the sequence.
     */
    public int getSequence() {
        return sequence;
    }
    /**
     * Increase the transfer sequence by 1.
     */
    public void increaseSequence() {
        this.sequence++;
    }
    /**
     * Set the sequence of the transfer.
     * @param sequence the value.
     */
    public void setSequence(int sequence) {
        this.sequence = sequence;
    }
    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        buf.append(id.toString());
        buf.append(" Status:" + status);
        buf.append(" Filesize:" + filesize);
        buf.append(" Offset:" + offset);
        buf.append(" Datasize:" + (data != null ? data.length : "null"));
        buf.append(" Sequence:" + sequence);
        return buf.toString();
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((timestamp == null) ? 0 : timestamp.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Transfer other = (Transfer) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (timestamp == null) {
            if (other.timestamp != null)
                return false;
        } else if (!timestamp.equals(other.timestamp))
            return false;
        return true;
    }
}
